home *** CD-ROM | disk | FTP | other *** search
- /* Includes */
- #include <QDOffscreen.h>
- #include <Palettes.h>
-
- /* Defines */
- #define TOTALCOLORS 256
- #define WWIDTH (TOTALCOLORS * 2)
- #define WHALFWIDTH TOTALCOLORS
- #define WHEIGHT TOTALCOLORS
- #define WLEFT (((qd.screenBits.bounds.right - qd.screenBits.bounds.left) - WWIDTH) / 2)
- #define WTOP (((qd.screenBits.bounds.bottom - qd.screenBits.bounds.top) - WHEIGHT) / 2)
-
- /* Global Variable Definitions */
- #ifdef powerc
- QDGlobals qd;
- #endif
- WindowPtr gWindow;
- PaletteHandle gPalette;
- GWorldPtr gRGBGWorld;
- GWorldPtr gPmGWorld;
- PixMapHandle gRGBPixMap;
- PixMapHandle gPmPixMap;
- CTabHandle gClut;
- Rect gRGBRect = { 0, 0, WHEIGHT, WHALFWIDTH };
- Rect gPmRect = { 0, WHALFWIDTH, WHEIGHT, WWIDTH };
- short gMode = srcOr;
- short gText = times;
- short gSize = 24;
-
- /* Procedure Prototypes */
- void initMac( void );
- void makePalette( void );
- void createWindow( void );
- void createOffscreens( void );
- void createRGBForeColorImage( void );
- void createPmForeColorImage( void );
- void drawWindow( void );
- void doEventLoop( void );
-
-
- /* */
- /* Main loop. */
- /* */
-
- main( void )
- {
- initMac();
-
- makePalette();
- createWindow();
- createOffscreens();
- createRGBForeColorImage();
- createPmForeColorImage();
-
- doEventLoop();
- }
-
-
- /* */
- /* Mac initialization. */
- /* */
-
- void initMac( void )
- {
- MaxApplZone();
- MoreMasters();
- MoreMasters();
- InitGraf( &qd.thePort );
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( (long)nil );
- InitCursor();
- FlushEvents( 0, everyEvent );
- }
-
-
- /* */
- /* Create the palette from the clut. */
- /* */
-
- void makePalette( void )
- {
- short plttID = 128;
-
- /* Get the color table from the .rsrc file. */
- gClut = GetCTable( plttID );
-
- /* Create the palette from the color table. */
- /* pmExplicit says we want the colors in the same order as the color table. */
- /* pmTolerant with a tolerance of 0 says we want exactly the same colors as the color table. */
- gPalette = NewPalette( TOTALCOLORS, gClut, pmExplicit + pmTolerant, 0 );
- }
-
-
- /* */
- /* Create the window to display the images and set the palette to it. */
- /* */
-
- void createWindow( void )
- {
- Rect bounds;
-
- SetRect( &bounds, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT );
-
- gWindow = NewCWindow( 0L, &bounds, "\pPalette&GWorld", true, documentProc,
- (WindowPtr)-1L, true, 0L );
- SetPort( gWindow );
-
- /* Attach the palette to the window. */
- NSetPalette( gWindow, gPalette, pmAllUpdates );
- }
-
-
- /* */
- /* Create the GWorlds based on the clut to hold the images. */
- /* */
-
- void createOffscreens( void )
- {
- CTabHandle ctable;
- GDHandle screensDevice;
- Rect area;
- short depth = 8;
-
- /* Set the color table's ctSeed equal to that of the main screen's GDevice. */
- /* This tells QuickDraw not to do color mapping. */
- /* Color mapping is not needed because the palette matches the color table. */
- screensDevice = GetMainDevice();
- if (screensDevice != nil)
- (**gClut).ctSeed = (**(**(**screensDevice).gdPMap).pmTable).ctSeed;
-
- /* Create the RGBForeColor offscreen world using the colortable. */
- NewGWorld( &gRGBGWorld, depth, &gRGBRect, gClut, nil, 0 );
- gRGBPixMap = GetGWorldPixMap( gRGBGWorld );
-
- /* Create the PmForeColor offscreen world using the colortable. */
- NewGWorld( &gPmGWorld, depth, &gPmRect, gClut, nil, 0 );
- gPmPixMap = GetGWorldPixMap( gPmGWorld );
- }
-
-
- /* */
- /* Create the GWorld image using Index2Color and RGBForeColor. */
- /* */
-
- void createRGBForeColorImage( void )
- {
- int i;
- CGrafPtr currentPort;
- GDHandle currentDevice;
- RGBColor color;
- FontInfo info;
- Str255 string = "\pRGBForeColor";
-
- GetGWorld( ¤tPort, ¤tDevice );
-
- SetGWorld( gRGBGWorld, nil );
- PenSize ( 1, 1 );
- LockPixels( gRGBPixMap );
-
- /* Show all the colors in the background using Index2Color and RGBForeColor. */
- for (i = 0; i < TOTALCOLORS; i++){
- Index2Color( i, &color );
- RGBForeColor( &color );
- MoveTo( i, 0 );
- Line( 0, WHEIGHT );
- }
-
- /* Draw label; RGBForeColor should now be black. */
- TextMode( gMode );
- TextFont( gText );
- TextSize( gSize );
- GetFontInfo( &info );
- MoveTo( (WHALFWIDTH - StringWidth(string))/2, WHEIGHT/2 - info.ascent/3 );
- DrawString( string );
-
- UnlockPixels( gRGBPixMap );
- SetGWorld( currentPort, currentDevice );
- }
-
-
- /* */
- /* Create the GWorld image by setting the palette to the GWorld and using PmForeColor. */
- /* */
-
- void createPmForeColorImage( void )
- {
- int i;
- CGrafPtr currentPort;
- GDHandle currentDevice;
- FontInfo info;
- Str255 string = "\pPmForeColor";
-
- GetGWorld( ¤tPort, ¤tDevice );
-
- SetGWorld( gPmGWorld, nil );
- NSetPalette( (WindowPtr)gPmGWorld, gPalette, pmNoUpdates );
- PenSize ( 1, 1 );
- LockPixels( gPmPixMap );
-
- /* Show all the colors in the background using PmForeColor. */
- for (i = 0; i < TOTALCOLORS; i++){
- PmForeColor( i );
- MoveTo( (i + WHALFWIDTH), 0 );
- Line( 0, WHEIGHT );
- }
-
- /* Draw label; PmForeColor should now be black. */
- TextMode( gMode );
- TextFont( gText );
- TextSize( gSize );
- GetFontInfo( &info );
- MoveTo( ((WHALFWIDTH - StringWidth(string))/2) + WHALFWIDTH, WHEIGHT/2 - info.ascent/3 );
- DrawString( string );
-
- UnlockPixels( gPmPixMap );
- SetGWorld( currentPort, currentDevice );
- }
-
-
- /* */
- /* CopyBits both images to the screen. */
- /* */
-
- void drawWindow( void )
- {
- CopyBits( (BitMap*)*gRGBPixMap, &gWindow->portBits, &(**gRGBPixMap).bounds,
- &gRGBRect, srcCopy, 0l );
-
- CopyBits( (BitMap*)*gPmPixMap, &gWindow->portBits, &(**gPmPixMap).bounds,
- &gPmRect, srcCopy, 0l );
- }
-
-
- /* */
- /* Event loop. */
- /* */
-
- void doEventLoop( void )
- {
- EventRecord event;
- WindowPtr window;
- short clickArea;
- Rect screenRect;
-
- for (;;){
- if (WaitNextEvent( everyEvent, &event, 0, nil )){
- if (event.what == mouseDown){
- clickArea = FindWindow( event.where, &window );
-
- if (clickArea == inDrag){
- screenRect = (**GetGrayRgn()).rgnBBox;
- DragWindow( window, event.where, &screenRect );
- }
- else if (clickArea == inContent){
- if (window != FrontWindow())
- SelectWindow( window );
- }
- else if (clickArea == inGoAway)
- if (TrackGoAway( window, event.where ))
- return;
- }
- else if (event.what == updateEvt){
- window = (WindowPtr)event.message;
- SetPort( window );
- BeginUpdate( window );
- drawWindow();
- EndUpdate( window );
- }
- }
- }
- }